import '@/assets/style/global.less';
import ArcoVue from '@arco-design/web-vue';
import '@arco-design/web-vue/dist/arco.css';
import ArcoVueIcon from '@arco-design/web-vue/es/icon';
import { autoAnimatePlugin } from '@formkit/auto-animate/vue';
import { createApp } from 'vue';
import print from 'vue3-print-nb';
import App from './App.vue';

import directive from './directive';
import router from './router';
import store from './store';
/** 引入uno.css，不引入不生效 */
import { initRouterNode } from '@/hooks/roleRouter';
import storage from '@/utils/storage';
import '@pickmall/ui-v3-components/dist/style.css';
import { ColorPicker as TColorPicker } from 'tdesign-vue-next';
import 'uno.css';
// 引入TDesign的内容用于 colorpicker guide 等
import 'tdesign-vue-next/es/style/index.css';

import { decrypt } from '@/utils/jsencrypt';

const app = createApp(App);
// 新增新页面打开
app.config.globalProperties.$openWindow = (path: any) => {
  const routerUrl = router.resolve(path);
  window.open(routerUrl.href, '_blank');
};

declare module 'vue' {
  export interface ComponentCustomProperties {
    $openWindow: any;
  }
}

const getTokenFromUrl = () => {
  const urlParams = new URLSearchParams(window.location.search);
  const newToken = urlParams.get('_t');
  if (newToken) {

    storage.clearToken();
    storage.setAccessToken(decrypt(newToken));
    // 移除 URL 中的 token 参数，避免刷新页面后重复处理
    const newUrl = window.location.href.split('?')[0];
    window.history.replaceState({}, '', newUrl);
  }
};

const starter = async () => {
  getTokenFromUrl()
  if (storage.getAccessToken()) {
    await initRouterNode(true);
  }
  app.use(router).use(directive).mount('#app');
};
app.use(TColorPicker), app.use(ArcoVue, {});
app.use(ArcoVueIcon);
app.use(autoAnimatePlugin);
app.use(store);
app.use(print);
starter();
